﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using System.Web;
using YouHP.Model.Group;

namespace web.xml
{
    public partial class treatmentlist_xml : System.Web.UI.Page
    {
        protected PatientRecordInfo PrData;
        #region 用户基本信息
        protected UserInfo UserData = new UserInfo();// 当前用户的信息
        protected UserGroupInfo UserGroupInfo = new UserGroupInfo(); //当前用户组信息   
        #endregion

        protected List<TreatmentInfo> TreatmentDataList;
        protected List<DiseaseInfo> DiseaseLst;
        protected int TreatmentCount = 0;
        protected int TreatmentPageCount = 0;
        protected int TreatmentPageSize = 10;
        protected int TreatmentPageIndex = 1;
        protected string TreatmentPagingString;

        protected TreatmentSearchCondition cond = null;
        protected void ResponseContext(int iCode, string sMsg)
        {
            //生产xml文档
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");
            Response.ContentType = "text/xml";
            using (XmlWriter wr = XmlWriter.Create(Response.OutputStream, settings))
            {
                // Write XML data.
                wr.WriteStartElement("result");
                wr.WriteAttributeString("code", iCode.ToString());
                wr.WriteAttributeString("msg", sMsg);
                if (iCode == 0)
                {
                    wr.WriteStartElement("treatments");
                    wr.WriteAttributeString("count", TreatmentCount.ToString());
                    foreach (TreatmentInfo info in TreatmentDataList)
                    {
                        wr.WriteStartElement("treatment");
                        wr.WriteAttributeString("id", info.Id.ToString());
                        wr.WriteAttributeString("name", info.Name.ToString());
                        wr.WriteAttributeString("desc", info.Intro);
                        wr.WriteAttributeString("count", info.PatientCount.ToString());
                        wr.WriteAttributeString("generalname", info.GeneralName);
                        wr.WriteEndElement();
                    }
                }
                wr.WriteEndElement();
                wr.WriteEndElement();
                wr.Flush();
            }
        }
        protected bool CheckLogin()
        {
            UserData.Id = CookiesTools.GetUserIdCookie();
            if (UserData.Id > 0) //表示永久登陆的用户
            {
                UserData.UserName = CookiesTools.GetUserNameCookie();   //直接从cookie中获取用户名
                if (UserData.UserName.Length <= 0)
                {
                    UserData.Id = -1;    //如果不存在用户名，则为无效的cookie
                    return false;
                }
            }
            return true;
        }
        protected bool CheckParam(ref string sMsg)
        {
           // int topiccnt = 8;

            TreatmentPageIndex = RequestTools.GetFormInt("pno", 1);
            TreatmentPageSize = RequestTools.GetFormInt("pnum", 10);
            cond = new TreatmentSearchCondition();
            cond.DiseaseId = RequestTools.GetFormInt("dszid", -1);
            cond.DiseaseName = RequestTools.GetFormString("dszname").Trim();
            cond.TreatmentName = RequestTools.GetFormString("trmname").Trim();
            cond.FirstLetter = RequestTools.GetFormString("firstLetter").Trim();
            return true;
        }
        protected void Handle()
        {
            //int topiccnt = 8;

            if (!CheckLogin())
            {
                ResponseContext(-1000, "你还没有登录");
                return;
            }
            string sErrMsg = "";
            if (!CheckParam(ref sErrMsg))
            {
                ResponseContext(-1001, sErrMsg);
                return;
            }


            PrData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);

            if (PrData.Id <= 0)
            {
                ResponseContext(-100, "请先填写患病基本信息");
                return;
            }

            string sOrder = "id";
            if (cond.DiseaseId == -1)
            {
                if( cond.FirstLetter == "" && cond.TreatmentName == "" && cond.DiseaseName == "" )
                {
                    cond.DiseaseId = PrData.DiseaseId;
                    TreatmentDataList = TreatmentBLL.Instance.GetTreatmentListOrderByDiseaseId(PrData.DiseaseId,TreatmentPageSize,TreatmentPageIndex,out TreatmentCount);
                }
                else
                {
                    TreatmentDataList = TreatmentBLL.Instance.GetTreatmentListWithOrder(
                    TreatmentPageSize, TreatmentPageIndex,
                     cond, sOrder, out TreatmentCount);
                }
                
            }
            else
            {
                TreatmentDataList = TreatmentBLL.Instance.GetTreatmentListWithOrder(
                TreatmentPageSize, TreatmentPageIndex,
                cond, sOrder, out TreatmentCount);
            }
            

            ResponseContext(0, "成功");

        }


        protected void Page_Load(object sender, EventArgs e)
        {
            Handle();
        }
    }
}
